package pl.edu.agh.io.sylvia.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import pl.edu.agh.io.sylvia.entity.Group;
import pl.edu.agh.io.sylvia.entity.Keyword;
import pl.edu.agh.io.sylvia.entity.Monitor;



public class MonitorDAO extends GenericDAO {

	public List<Monitor> getByArticleId(int id) {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Monitor> results= new ArrayList<Monitor>();
		
		try {
			statement = connection.prepareStatement("SELECT * FROM MONITORS JOIN MONITORS_DETAILS ON MONITORS_DETAILS.MONITORS_ID = MONITORS.ID WHERE MONITORS_DETAILS.ARTICLES_ID = ?");
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			
			KeywordDAO keywordDAO = new KeywordDAO();
			GroupDAO groupDAO = new GroupDAO();
			
			while(resultSet.next()) {
				int monitorId = resultSet.getInt("id");
				String name = resultSet.getString("name");
				String email = resultSet.getString("email");
				int howOften = resultSet.getInt("howoften");
				results.add(new Monitor(monitorId, name, email, howOften, groupDAO.getByMonitorId(monitorId), keywordDAO.getByMonitorId(monitorId)));
			}
			return results;
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}
	
	@Override
	public List<Monitor> getAll() {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Monitor> results= new ArrayList<Monitor>();
		
		try {
			statement = connection.prepareStatement("SELECT * FROM MONITORS");
			resultSet = statement.executeQuery();
			
			GroupDAO groupDAO = new GroupDAO();
			KeywordDAO keywordDAO = new KeywordDAO();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				String email = resultSet.getString("email");
				int howOften = resultSet.getInt("howoften");
				results.add(new Monitor(id, name, email, howOften, groupDAO.getByMonitorId(id), keywordDAO.getByMonitorId(id)));
			}
			return results;
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}

	@Override
	public Monitor getById(int id) {
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
		try {
			statement = connection.prepareStatement("SELECT * FROM MONITORS WHERE id = ?");
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			
			GroupDAO groupDAO = new GroupDAO();
			KeywordDAO keywordDAO = new KeywordDAO();
			
			if (resultSet.next()) {
				String name = resultSet.getString("name");
				String email = resultSet.getString("email");
				int howOften = resultSet.getInt("howoften");
				List<Group> groups = groupDAO.getByMonitorId(id);
				List<Keyword> keywords = keywordDAO.getByMonitorId(id);
				return new Monitor(id, name, email, howOften, groups, keywords);
			} else {
				return null;
			}
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			e.printStackTrace();
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}
	
	public Monitor getByName(String name){
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
		try {
			statement = connection.prepareStatement("SELECT * FROM MONITORS WHERE name = ?");
			statement.setString(1, name);
			resultSet = statement.executeQuery();
			
			if (resultSet.next()) {
				int id = resultSet.getInt("id");
				return getById(id);
			} else {
				return null;
			}
		} catch (SQLException e) {
			log.log(Level.SEVERE, e.getMessage(), e);
			e.printStackTrace();
			return null;
		} finally {
			if (resultSet != null) try {
				resultSet.close();
			} catch(SQLException e) {};
			if (statement != null) try {
				statement.close();
			} catch (SQLException e) {};
		}
	}

}
